<?php

class Social_Adapter_Live extends Social_Adapter_OAuthAbstract
{

	/**
	 * @var string
	 */
	protected $_serviceId = 'live';

	/**
	 * @return void
	 */
	function __construct()
	{
		require_once LEGACY_DIR . 'social/live/windowslivelogin.php';

	}

	function getClientApi()
	{

		if (NULL == $this -> _clientApi)
		{
			$key = Nuo::getConfig('live','key');
			$secret = Nuo::getConfig('live','secret');
			$debug = Nuo::getConfig('live','debug');
			$offers = Nuo::getConfig('offers');

			$o = new WindowsLiveLogin();

			$o -> setAppId($key);
			$o -> setSecret($secret);
			$o -> setDebug($debug);

			$returnUrl = $this -> getCallbackUrl();
			$policyUrl = 'http://namnv.younetco.com/soap/social/live/policy';

			$o -> setSecurityAlgorithm('wsignin1.0');
			$o -> setReturnUrl($returnUrl);
			$o -> setPolicyUrl($policyUrl);

			/**
			 * @see WindowsLiveLogin::initFromXML
			 */
			//$o -> setForceDelAuthNonProvisioned(true);
			//$o -> setOldSecret(@$settings['oldsecret']);
			//$o -> setOldSecretExpiry(@$settings['oldsecretexpiry']);

			//$o -> setBaseUrl(@$settings['baseurl']);
			//$o -> setSecureUrl(@$settings['secureurl']);
			//$o -> setConsentBaseUrl(@$settings['consenturl']);
			$this -> _clientApi = $o;
		}

		return $this -> _clientApi;
	}

	function getUserInfo($token)
	{
		$uri = 'https://apis.live.net/v5.0/me?access_token=' . $token -> getDelegationToken();

		// $uri =
		// "https://livecontacts.services.live.com/users/@C@".$output."/LiveContacts";

		$curl = new Nuo_Curl();

		$headers = array("Authorization: DelegatedToken dt=\"" . $token -> getDelegationToken() . "\"");
	
		$response = $curl -> send($uri, 'GET', NULL, $headers);

		$data = json_decode($response);

		return $data;
	}

	function getContacts($token)
	{
		// Convert Unix epoch time stamp to user-friendly format.
        $expiry = $token -> getExpiry();
        $expiry = date(DATE_RFC2822, $expiry);
		
        //*******************CONVERT HEX TO DOUBLE LONG INT
        // ***************************************
        $hexIn = $token -> getLocationID();
        include LEGACY_DIR . "social/live/hex.php";
        $longint = $output;
        //here's the magic long integer to be sent to the Windows Live service

        //*******************CURL THE REQUEST
        // ***************************************
        $uri = "https://livecontacts.services.live.com/users/@C@" . $output . "/LiveContacts";
		
		$headers = array("Authorization: DelegatedToken dt=\"" . $token -> getDelegationToken() . "\"");

        $curl = new Nuo_Curl;
		
		$response  = $curl->send($uri, 'GET',NULL, $headers);
		
        if (!$response)
        {
            echo $uri;
            die ;
        }

        $data = json_decode(json_encode(simplexml_load_string($response)), 1);
		
		$contacts = array();

        if (isset($data['Contacts']['Contact']))
        {
			$arrContacts = $data['Contacts']['Contact'];
			
			if(isset($arrContacts['ID'])){
				$arrContacts = array($arrContacts);
			}

            foreach ($arrContacts as $contact)
            {
				
                $name = $email = null;
                $name =  @$contact['Profiles']['Personal']['DisplayName'];
                $email = @$contact['Emails']['Email']['Address'];
				
                if($email  == '')
                {
                    continue;    
                }
                
                if(null == $name or $name ==  ' ' or $name  ==  '  '){
                    $name =  $email;
                }

                $contacts[] = array(
                    'name' => $name,
                    'email' => $email
                );
            }
        }
		
		var_dump($contacts);
		exit;
		
		return $contacts;
	}

}
